//
//  CashBackViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/16.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class CashBackViewModel {
    
    private var provider: RxMoyaProvider<CashBackApi>
    
    init(token: String) {
        let endpointClosure = { (target: CashBackApi) -> Endpoint<CashBackApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json", "Authorization": "Bearer \(token)"])
        }
        provider = RxMoyaProvider<CashBackApi>(endpointClosure: endpointClosure)
    }

    func getUserCashBack() -> Observable<UserCashBack> {
        return provider.request(.getUserCashBack())
               .mapJSON()
               .mapObject(type: UserCashBack.self)
    }
    
    func getOrderCashBack(id: Int) -> Observable<OrderCashBack> {
        return provider.request(.getOrderCashBack(id: id))
               .mapJSON()
               .mapObject(type: OrderCashBack.self)
    }
}
